//
//  GroupHeaderView.m
//  QQ界面
//
//  Created by apple on 16/11/18.
//  Copyright © 2016年 LWB. All rights reserved.
//

#import "GroupHeaderView.h"
#import "FriendGroup.h"
@interface GroupHeaderView()
@property (nonatomic,strong)UIButton *btnGroupTitle;
@property(nonatomic, strong)UILabel *lblCount;
//@property(nonatomic,strong)UIImageView *icon;

@end
@implementation GroupHeaderView
//实现类初始化方法，创建一个headrview;
+(instancetype)groupHeaderViewWithTableView:(UITableView *)tableView{
    static NSString *ID = @"group_header_view";
    GroupHeaderView *headVw = [tableView dequeueReusableHeaderFooterViewWithIdentifier:ID];
    if (headVw ==nil) {
        headVw  = [[GroupHeaderView alloc]initWithReuseIdentifier:ID];
    }
    return headVw;
}

-(instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        UIButton *btnGroupTitle = [[UIButton alloc]init];
        [btnGroupTitle setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btnGroupTitle setImage:[UIImage imageNamed:@"buddy_header_arrow"] forState:UIControlStateNormal];
        [btnGroupTitle setBackgroundImage:[UIImage imageNamed:@"5F85CCF2-9E09-41FE-8969-08A426E02052"] forState:UIControlStateNormal];
//        [btnGroupTitle setBackgroundColor:[UIColor whiteColor]];
        [btnGroupTitle setBackgroundImage:[UIImage imageNamed:@"buddy_header_bg_highlighted"] forState:UIControlStateHighlighted];
        [btnGroupTitle setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
        //按钮内容的距离边界的距离
        btnGroupTitle.contentEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
        //设置按钮标题和图标之间的间距
        btnGroupTitle.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
        //按钮的点击事件
        [btnGroupTitle addTarget:self action:@selector(btnGroupTitleClick) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:btnGroupTitle];
        _btnGroupTitle = btnGroupTitle;
        //添加在线人数的标签
        UILabel *lblCount = [[UILabel alloc]init];
        [self.contentView addSubview:lblCount];
        _lblCount = lblCount;
        //设置图片旋转模式为中心旋转
        _btnGroupTitle.imageView.contentMode = UIViewContentModeCenter;
        //图片的超出部分不截掉
        btnGroupTitle.imageView.clipsToBounds = NO;
    }
    return self;
}
-(void)btnGroupTitleClick{
    _group.visible =!_group.isVisible;
    //处理怎么展开或者收起
    //代理来调用展开方法，判断我的代理是否实现我的协议方法
    if ([_delegatep respondsToSelector:@selector(groupHeaderViewClieckTitleButton:)]) {
        [_delegatep groupHeaderViewClieckTitleButton:self];
    }else{
        NSLog(@"代理者未实现协议方法");
    }
}
//重写group属性的set方法，设置控件的数据和frame
-(void)setGroup:(FriendGroup *)group{
    _group = group;
    //设置控件数据
    [_btnGroupTitle setTitle:group.name forState:UIControlStateNormal];
    //设置在线人数的数据
    NSString *textStr = [NSString stringWithFormat:@"%d/%lu",group.online,(unsigned long)group.friends.count];
    _lblCount.text = textStr;
    if (_group.isVisible) {
        _btnGroupTitle.imageView.transform = CGAffineTransformMakeRotation(M_PI_2);
    }else{
        _btnGroupTitle.imageView.transform = CGAffineTransformMakeRotation(0);
    }
    //    //设置frame
    //    _btnGroupTitle.frame = self.bounds;
    //    _lblCount.frame =CGRectMake(self.frame.size.width - 70, 0, 60, self.frame.size.height);
}
//- (void)didMoveToSuperview
//{
//    _btnGroupTitle.imageView.transform =_group.isVisible ? CGAffineTransformMakeRotation(M_PI_2) : CGAffineTransformMakeRotation(0);
//}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    _btnGroupTitle.frame = self.bounds;
    _lblCount.frame = CGRectMake(self.frame.size.width - 70, 0, 60, self.frame.size.height);
}

@end
